<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学员约课</el-breadcrumb-item>
      <el-breadcrumb-item>约课列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图 -->
    <el-card>
      <!-- 约课列表区域 -->
      <el-table :data="courseList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="用户id" prop="id" v-if="false"></el-table-column>
        <el-table-column label="教练" prop="coachName" width="200px"></el-table-column>
        <el-table-column label="时间段" prop="coursePeriod" width="200px"></el-table-column>
        <el-table-column label="日期" prop="courseDate" width="150px"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 取消课程按钮 -->
            <el-button size="mini" type="primary" icon="el-icon-remove"
                       @click="cancelCourse(scope.row.id)">取消课程</el-button>
            <!-- 查看教练信息按钮 -->
            <el-button size="mini" type="primary" icon="el-icon-search"
                       @click="lookCoach(scope.row.coachId)">查看教练信息</el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-card>

    <!-- 教练信息 -->
    <el-dialog :visible.sync="courseCoachDialogVisible" width="50%">
      <el-descriptions title="教练信息" :column="2" border>
        <el-descriptions-item label="姓名">{{ courseCoach.name }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ courseCoach.sex }}</el-descriptions-item>
        <el-descriptions-item label="驾龄">{{ courseCoach.drivingAge }} 年</el-descriptions-item>
        <el-descriptions-item label="教龄">{{ courseCoach.coachAge }} 年</el-descriptions-item>
        <el-descriptions-item label="历史学生总数">{{ courseCoach.studentsAllNumber }}</el-descriptions-item>
        <el-descriptions-item label="考试通过率">{{ courseCoach.studentPassRate }}</el-descriptions-item>
        <el-descriptions-item label="教练证" v-if="courseCoach.coachLicense == '已拥有'">{{ courseCoach.coachLicenseClass }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "Course",
  created() {
    this.getStudentCourse()
  },
  data() {
    return {
      // 已预约课程
      courseList: [],
      // 教练信息
      courseCoach: {},
      // 控制教练信息对话框的显示与隐藏
      courseCoachDialogVisible: false,
    }
  },
  methods: {
    getStudentCourse(){
      this.$http.post('/driver/course/student/course').then(res => {
        if (res.data.code === 200) {
          this.courseList = res.data.data
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    },



    // 取消预约课程
    cancelCourse(id){
      this.$confirm('确定取消课程？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.get("/driver/course/delete", {
          params: {
            id: id
          }
        }).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.getStudentCourse()
          } else {
            this.$message.error(res.data.message)
          }
        }).catch(err => {
          console.log(err)
          this.$message.error(err.message)
        })
      })
    },
    // 查看教练信息
    lookCoach(id){
      this.$http.get('/driver/course/course/coach', {
        params: {
          coachId: id
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.courseCoach = res.data.data
          if (this.courseCoach.studentsAllNumber !== 0){
            this.courseCoach.studentPassRate = (100*this.courseCoach.studentsPassNumber/this.courseCoach.studentsAllNumber).toFixed(2)+"%"
          }
          this.courseCoachDialogVisible = true
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
